package org.itboys.core.controller.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.core.entity.mysql.SundayProduct;
import org.itboys.core.service.mysql.front.SundayFrontProductCategoryService;
import org.itboys.core.service.mysql.web.SundayProductService;

import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_controller_web
 */
@Controller
@RequestMapping("/sunday/web/product")
public class SundayProductController extends BaseController {
    @Autowired
    private SundayProductService productService;
    @Autowired
    private SundayFrontProductCategoryService categoryService;

    /**
     * 商品  主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        Model model) {
        return "/sunday/product/index";
    }

    /**
     * @param request
     * @return
     */
    @RequiresPermissions("product:input")
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value = "id", required = true) Long id,
            Model model
    ) {
        SundayProduct product = productService.findOne(id);
        model.addAttribute("product", product);
        return "/sunday/product/input";
    }

    /**
     * 分页查询商品
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response
    ) throws Exception {
        PageResult<SundayProduct> result = productService.selectProduct(request);
        JsonPageUtils.renderJsonPage(result.getTotal(), result.getData(), response);
    }

    /**
     * 新增或修改商品
     * @param request
     * @param response
     * @param product
     * @param defaultImageFile
     * @param rollImageFiles
     */

    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ModelAttribute SundayProduct product,
                     @RequestParam(value = "defaultImageFile", required = false) MultipartFile defaultImageFile,
                     @RequestParam(value = "backImageFile", required = false) MultipartFile backImageFile,
                     @RequestParam(value = "rollImageFiles", required = false) MultipartFile[] rollImageFiles
    ) throws Exception {
        productService.save(product, defaultImageFile, backImageFile, rollImageFiles);
        AjaxUtils.renderJsonporJson(response, null,
                ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
    }



    /**
     * 逐个删除某个商品
     *
     * @param request
     * @param response
     * @param id
     */
    @RequiresPermissions("product:delete")
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value = "id", required = true) Long id
    ) {
        productService.delete(id);
        AjaxUtils.renderJsonporJson(response, null,
                ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
    }

    /**
     * 产品选择器
     *
     * @param request
     * @param isSingle
     * @param param
     * @param model
     * @return
     */
    @RequestMapping("/choose")
    public String choose(HttpServletRequest request,
                         @RequestParam(value = "isSingle", required = true) Integer isSingle,//是否单选
                         @RequestParam(value = "callBack", required = false) String callBack,//回调参数
                         @RequestParam(value = "param", required = false) String param,//查询参数
                         @RequestParam(value = "chooseId", required = false) String chooseId,//选择器id（当页面存在多个选择器时使用该字段区分）

                         Model model) {
        model.addAttribute("param", param);
        model.addAttribute("callBack", callBack);
        model.addAttribute("chooseId", chooseId);
        if (isSingle == null) {
            return "/sunday/product/choose/choose_erro";
        } else if (isSingle == 1) {
            return "/sunday/product/choose/choose_single";
        } else if (isSingle == 0) {
            return "/sunday/product/choose/choose_muilt";
        }

        return "/finance/product/choose/choose_erro";
    }


}
